
var stepOneSelectProvince  =  "";
var stepOneSelectDistrict =  "";
var stepTwoSelectProvince = "";
var stepTwoSelectDistrict = "";
var stepTwoSelectProvinceContact = "";
var stepTwoSelectDistrictContact = "";
	

// JavaScript Document
$(document).ready(function(e) {
	
	$("#stepOneInputProvinceCode").change(function() {
		stepOneSelectProvince  =  $(this).val();
		district(stepOneSelectProvince, "", "stepOneInputDistrictCode");
		$("#stepOneInputDistrictCode").focus();
	});
	
	$("#stepOneInputDistrictCode").change(function(){
		stepOneSelectDistrict  =  $(this).val();
		cityCode(stepOneSelectProvince, stepOneSelectDistrict, "", "stepOneInputCityCode");
		$("#stepOneInputCityCode").focus();
	});
	
	
	$("#stepTwoInputProvinceCode").change(function() {
		stepTwoSelectProvince =  $(this).val();
		district(stepOneSelectProvince, "", "stepTwoInputDistrictCode");
		$("#stepTwoInputDistrictCode").focus();
	});
	
	$("#stepTwoInputDistrictCode").change(function(){
		stepTwoSelectDistrict  =  $(this).val();
		cityCode(stepOneSelectProvince, stepOneSelectDistrict, "", "stepTwoInputCityCode");
		$("#stepTwoInputCityCode").focus();
	});
	
	
	$("#stepTwoInputProvinceCodeContact").change(function() {
		stepTwoSelectProvinceContact  =  $(this).val();
		district(stepTwoSelectProvinceContact, "", "stepTwoInputDistrictCodeContact");
		$("#stepTwoInputDistrictCodeContact").focus();
	});
	
	$("#stepTwoInputDistrictCodeContact").change(function(){
		stepTwoSelectDistrictContact  =  $(this).val();
		cityCode(stepTwoSelectProvinceContact, stepTwoSelectDistrictContact, "", "stepTwoInputCityCodeContact");
		$("#stepTwoInputCityCodeContact").focus();
	});
	
	setDefaultAddress();
	
	customerClass("" , "stepThreeInputCustomerClass");
	meterSize("" , "stepThreeInputMeterSize");
	
	// Calculate Money
	
	$("#stepThreeInputMeterSize").change(function(){

		calculateMoney("Y",1);

	});
	
	$( "input[name=stepThreeInputCalculateMoney]" ).on( "click", function() {
		 var data = $( "#stepThreeInputCalculateMoney:checked" ).val();
		 if(data == "Y"){
			calculateMoney(data,2);
		 } else{ 
			 $("#DisplayCalculateMoney").html("");
		 }
	});
	
	
	// ===============================================================================================================================================
	
	$('.step-link').on('click',function() {
		var boxID = $(this).data('link');
		$('.form-wizard-box').removeClass('active');
		$(this).children().addClass('active');
		// ------- add
		$('.step-link').removeClass('active');
		$(this).addClass('active');
		// ------- end add
		$('.form-step').fadeOut('slow',function() {
			setTimeout(function() {
				$('#'+boxID).fadeIn('slow');
			},300);
		});
	});
	
	// checkLoadDataStep2
	$("#checkLoadStep2").click(function() {
	    var $this = $(this);
	    if ($this.is(':checked')) {
	    	loadDataStepTwo();
	    } else  { 
	    	
	    }
	});
	
	$("#checkLoadDataContact").click(function(){
		var  $this = $(this);
		if($this.is(":checked")){
			loadDataStepTwo2();
		}
	});
	
	// Load Province 
	$("#btn-add-step-one").click(function(){
		//alert("click1");
		var btn = $("#btn").val('1');
		$('#confirmSaveData').modal();

	});
	
	$("#btn-add-step-two").click(function(){
		//alert("click2");
		var btn = $("#btn").val('2');
		$('#confirmSaveData').modal();

	});
	
	$("#btn-add-step-three").click(function(){
		//alert("click3");
		var btn = $("#btn").val('3');
		$('#confirmSaveData').modal();

	});
	
});

//set Default Address
function setDefaultAddress(){	
	$.getJSON('CS61001Srvl', { vaDefaultAddress: "yes" }, function(result) {
		$("#stepOneInputPostCode").val(result.vaDefaultAddressArray[3]);
		$("#stepTwoInputPostCode").val(result.vaDefaultAddressArray[3]);
		$("#stepTwoInputPostCodeContact").val(result.vaDefaultAddressArray[3]);
		setTimeout( function(){ province( result.vaDefaultAddressArray[0],"stepOneInputProvinceCode");}, 300);
		setTimeout( function(){ province( result.vaDefaultAddressArray[0],"stepTwoInputProvinceCode");}, 300);
		setTimeout( function(){ province( result.vaDefaultAddressArray[0],"stepTwoInputProvinceCodeContact");}, 300);
		setTimeout( function(){ district( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],"stepOneInputDistrictCode");}, 300);
		setTimeout( function(){ district( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],"stepTwoInputDistrictCode");}, 300);
		setTimeout( function(){ district( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],"stepTwoInputDistrictCodeContact");}, 300);
		setTimeout( function(){ cityCode( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],result.vaDefaultAddressArray[2],"stepOneInputCityCode");}, 300);
		setTimeout( function(){ cityCode( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],result.vaDefaultAddressArray[2],"stepTwoInputCityCode");}, 300);
		setTimeout( function(){ cityCode( result.vaDefaultAddressArray[0],result.vaDefaultAddressArray[1],result.vaDefaultAddressArray[2],"stepTwoInputCityCodeContact");}, 300);
	});
}

/**
 * this function add data
 */
function saveData(){
	var btn = $("#btn").val();
	var process_type = $("#process_type").val();
	var request_number = $("#inputKeyWordSearch").val();
	
	$('#error_message').addClass('hide');
	$('#success_message').addClass('hide');
	
	// step1
	var stepOnePName = $("#stepOneInputPName").val();
	var stepOneName  = $("#stepOneInputName").val();
	var stepOneHomeNumber = $("#stepOneInputHomeNumber").val();
	var stepOneAddress = $("#stepOneInputAddress").val();
    var stepOneCityCode = $("#stepOneInputCityCode option:selected").val();
    var stepOneDistrictCode  = $("#stepOneInputDistrictCode option:selected").val();
    var stepOneProvinceCode  = $("#stepOneInputProvinceCode option:selected").val();
    var stepOnePostCode  = $("#stepOneInputPostCode").val();
    var stepOneTelephone  = $("#stepOneInputTelephone").val();
   
	// step2
	var stepTwoPname =  $("#stepTwoInputPname").val();
	var stepTwoFname = $("#stepTwoInputFname").val();
	var stepTwoLname = $("#stepTwoInputLname").val();
	var stepTwoIdCard = $("#stepTwoInputIdCard").val();
	var stepTwoHomeCardId = $("#stepTwoInputHomeCardId").val();
	var stepTwoHomeNumber = $("#stepTwoInputHomeNumber").val();
	var stepTwoAddress = $("#stepTwoInputAddress").val();
	var stepTwoCityCode = $("#stepTwoInputCityCode option:selected").val();
	var stepTwoDistrictCode = $("#stepTwoInputDistrictCode option:selected").val();
	var stepTwoProvinceCode = $("#stepTwoInputProvinceCode option:selected").val();
	var stepTwoPostCode = $("#stepTwoInputPostCode").val();
	var stepTwoTelephoneUser = $("#stepTwoInputTelephoneUser").val();
	
	var stepTwoNameContact = $("#stepTwoInputNameContact").val();
	var stepTwoHomeNumberContact = $("#stepTwoInputHomeNumberContact").val();
	var stepTwoAddressContact = $("#stepTwoInputAddressContact").val();
	var stepTwoCityCodeContact  = $("#stepTwoInputCityCodeContact option:selected").val();
	var stepTwoDistrictCodeContact  = $("#stepTwoInputDistrictCodeContact option:selected").val();
	var stepTwoProvinceCodeContact  = $("#stepTwoInputProvinceCodeContact option:selected").val();
	var stepTwoPostCodeContact = $("#stepTwoInputPostCodeContact").val();
	var stepTwoTelephoneContact = $("#stepTwoInputTelephoneContact").val();
	
	var stenTwoGarbageCharge = $("#stenTwoInputGarbageCharge").val();
	var stepTwoWasteWaterCharge = $("#stepTwoInputWasteWaterCharge").val();
	var stepTwoOtherCharge = $("#stepTwoInputOtherCharge").val();
	
    // step3
	var stepThreeCustomerType  =  $("#stepThreeInputCustomerType").val();
	var stepThreeCustomerClass = $("#stepThreeInputCustomerClass").val();
	var stepThreeAllowance = $("#stepThreeInputAllowance option:selected").val();
	
	var stepThreeComment = $("#stepThreeInputComment").val();
	var stepThreeMeterSize = $("#stepThreeInputMeterSize").val();
	var stepThreeCalculateMoney  = $("#stepThreeInputCalculateMoney").val();
	
	var stepOneStatus = false;
	var stepTwoStatus  = false;
	var stepThreeStatus  = false;
	
	if(stepOneName != "" && stepOneHomeNumber != "" && stepOneCityCode != "" && stepOneDistrictCode != ""  &&  stepOneProvinceCode != "" && stepOnePostCode != ""){
		stepOneStatus = true;
	} else{
		stepOneStatus = false;
	}

	if(stepTwoFname != "" &&  stepTwoLname != "" && stepTwoIdCard != "" && stepTwoHomeCardId != "" && stepTwoHomeNumber  != "" && stepTwoCityCode != "" && stepTwoDistrictCode  != "" && stepTwoProvinceCode != "" && stepTwoPostCode != "" && stepTwoHomeNumberContact != "" && stepTwoCityCodeContact != "" && stepTwoDistrictCodeContact != "" && stepTwoProvinceCodeContact != "" && stepTwoPostCodeContact != "" ) { 
		stepTwoStatus  = true;
	}else{
		stepTwoStatus  = false;
	}
	
	if(stepThreeCustomerClass != "" && stepThreeMeterSize != ""){
		stepThreeStatus  = true;
	}else{
		stepThreeStatus  = false;
	}

	if(stepOneStatus && stepTwoStatus && stepThreeStatus){ 
		
		// get data calculate amount
		var getValAmount = [];
		var index = 0;
		$("input[name=stepThreeInputCalculateMoneyAmount]").each(function(){
			console.log(index + " <> " + $(this).val());
			getValAmount[index]  = $(this).val();
			index++;
		});
		
		var getDocument = [];
		var indexDoc = 0;
		$("input[name=stepThreeInputDocument]:checked").each(function(){
			getDocument[indexDoc] = $(this).val();
			indexDoc++;
		});
		
		var receiveCode = [];
		var indexReceiveCode = 0;
		$("input[name=stepThreeInputReceive]").each(function (){
			receiveCode[indexReceiveCode] = $(this).val();
			indexReceiveCode++;
		});

		$.ajax({
			dataType: "json",
			url : 'CS61001Srvl' , 
			type : "POST",
			data : {
				
				process_type : process_type,
				request_number : request_number,
				stepOneInputPName : stepOnePName , 
				stepOneInputName : stepOneName ,
				stepOneInputHomeNumber : stepOneHomeNumber , 
				stepOneInputAddress : stepOneAddress , 
				stepOneInputCityCode : stepOneCityCode , 
				stepOneInputDistrictCode : stepOneDistrictCode , 
				stepOneInputProvinceCode : stepOneProvinceCode , 
				stepOneInputPostCode :  stepOnePostCode , 
				stepOneInputTelephone : stepOneTelephone , 
			
				stepTwoInputPname : stepTwoPname , 
				stepTwoInputFname : stepTwoFname , 
				stepTwoInputLname : stepTwoLname , 
				stepTwoInputIdCard : stepTwoIdCard , 
				stepTwoInputHomeCardId : stepTwoHomeCardId , 
				stepTwoInputHomeNumber : stepTwoHomeNumber , 
				stepTwoInputAddress : stepTwoAddress , 
				stepTwoInputCityCode : stepTwoCityCode , 
				stepTwoInputDistrictCode : stepTwoDistrictCode , 
				stepTwoInputProvinceCode : stepTwoProvinceCode , 
				stepTwoInputPostCode : stepTwoPostCode , 
				
				stepTwoInputTelephoneUser : stepTwoTelephoneUser , 
				stepTwoInputNameContact : stepTwoNameContact , 
				stepTwoInputHomeNumberContact : stepTwoHomeNumberContact , 
				stepTwoInputAddressContact : stepTwoAddressContact , 
				stepTwoInputCityCodeContact : stepTwoCityCodeContact , 
				stepTwoInputDistrictCodeContact : stepTwoDistrictCodeContact , 
				stepTwoInputProvinceCodeContact : stepTwoProvinceCodeContact , 
				stepTwoInputPostCodeContact : stepTwoPostCodeContact , 
				stepTwoInputTelephoneContact : stepTwoTelephoneContact , 
				stenTwoInputGarbageCharge : stenTwoGarbageCharge , 
				stepTwoInputWasteWaterCharge : stepTwoWasteWaterCharge , 
				stepTwoInputOtherCharage : stepTwoOtherCharge , 
				stepThreeInputCustomerType : stepThreeCustomerType , 
				stepThreeInputCustomerClass : stepThreeCustomerClass , 
				stepThreeInputAllowance : stepThreeAllowance , 
				stepThreeInputDocument : getDocument , 
				stepThreeInputComment : stepThreeComment , 
				stepThreeInputMeterSize : stepThreeMeterSize , 
				stepThreeInputCalculateMoney : stepThreeCalculateMoney , 
				stepThreeInputCalculateMoneyAmount : getValAmount , 
				stepThreeInputReceiveCode : receiveCode
			} ,
			success: function(data){
				//alert(data);
				
				if(data.result == "success"){
					var vnUserId = $("#userId").val();
					var vnSiteId = $("#siteId").val();

					var url = reporturl+"CS61001R.jsp?siteId="+vnSiteId+"&userId="+vnUserId+"&requestNumber="+data.RequestNumber;
					window.open(url);
					
					$("html,body").animate({scrollTop: 0}, 100);
					$(window).scrollTop($("#success_message").offset().top);
					
					$("#inputKeyWordSearch").val('');
					$("#content_sucess").text("บันทึกข้อมูลเรียบร้อย");
					$('#success_message').removeClass('hide');
					document.getElementById("form-data-register").reset();
					$('input[name=stepThreeInputCalculateMoney][value=N]').prop("checked",true);
					$("#DisplayCalculateMoney").html("");
					$(":checkbox[name='stepThreeInputDocument']").attr('checked', false);
					
					var containnerId  = "form-1";
					$('.form-wizard-box').removeClass('active');
					$('.form-1').children().addClass('active');
					$('.step-link').removeClass('active');
					$('#form-tap-1 > div').addClass('active');
					$('.form-step').fadeOut('slow',function() {
						setTimeout(function() {
							$('#'+containnerId).fadeIn('slow');
						},300);
					});

					return false;
				} else { 
					$("html,body").animate({scrollTop: 0}, 100);
					$(window).scrollTop($("#error_message").offset().top);
					
					$("#content_error").text(" เกิดข้อผิดพลาดกรุณาตรวจสอบข้อมูล  ");
					$('#error_message').removeClass('hide');

					return false;
				}
			}
		});
		
	} else { 
		$("html,body").animate({scrollTop: 0}, 100);
		$(window).scrollTop($("#error_message").offset().top);

		if(!stepOneStatus && btn == 1){ 

			$("#content_error").text("กรอกข้อมูลไม่สมบูรณ์ กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย '*' ให้ครบถ้วน");
			$('#error_message').removeClass('hide');
			console.log("fail one.");
			return false;
		}else if(stepOneStatus && btn == 1){
			var containnerId  = "form-2";
			$('.form-wizard-box').removeClass('active');
			$('.form-2').children().addClass('active');
			$('.step-link').removeClass('active');
			$('#form-tap-2 > div').addClass('active');
			$('.form-step').fadeOut('slow',function() {
				setTimeout(function() {
					$('#'+containnerId).fadeIn('slow');
				},300);
			});
		}
		
		if(!stepTwoStatus && btn == 2){ 
	
			$("#content_error").text("กรอกข้อมูลไม่สมบูรณ์ กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย '*' ให้ครบถ้วน");
			$('#error_message').removeClass('hide');

			console.log("fail two.");
			return false;
		}else if(stepTwoStatus && btn == 2){

			var containnerId  = "form-3";
			$('.form-wizard-box').removeClass('active');
			$('.form-3').children().addClass('active');
			$('.step-link').removeClass('active');
			$('#form-tap-3 > div').addClass('active');
			$('.form-step').fadeOut('slow',function() {
				setTimeout(function() {
					$('#'+containnerId).fadeIn('slow');
				},300);
			});
		}
		
		if(!stepThreeStatus  && btn == 3){ 

			$("#content_error").text("กรอกข้อมูลไม่สมบูรณ์ กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย '*' ให้ครบถ้วน");
			$('#error_message').removeClass('hide');
		
			console.log("fail three.");
			return false;
		}
		
		
		
	}

}
/**
 * this get province display component
 * @param province
 * @param DisplayElectmentId
 */
function province(province , DisplayElectmentId) {
	$.getJSON('ProvinceComponentAPISrvl', {
		province: "yes",
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกจังหวัด --</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(province==result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		$("#"+DisplayElectmentId).html(options); 
	});
}

/**
 * this component district component
 * @param provinceId
 * @param districtId
 * @param displayElementId
 */
function district(provinceId , districtId ,  displayElementId){
	$.getJSON('DistrictComponentAPISrvl', {
		proVince: provinceId,
	}, function(result) {
		var options = '';
		options += '<option value="">-- เลือกอำเภอ --</option>';
		var selected ="";
		for (var i = 0; i < result.values.length; i++) {
			if(districtId == result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#" + displayElementId).html(options);
	});
}

/**
 * this component city Code
 * @param provinceId
 * @param districtId
 * @param cityCode
 * @param displayElectmentId
 */
function cityCode(provinceId , districtId , cityCode , displayElectmentId){
	$.getJSON('CityCodeComponentAPISrvl', {
		proVince: provinceId,
		disTrict: districtId,
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกตำบล--</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(cityCode == result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#"+displayElectmentId).html(options);
	});
}

/**
 * this is compronent customer class
 * @param customerClassId
 * @param displayElectmentId
 */
function customerClass(customerClassId , displayElectmentId){
	$.getJSON('CustomerClassComponentAPISrvl', {
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกประเภทผู้ใช้น้ำ--</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(customerClassId == result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#"+displayElectmentId).html(options);
	});
}

/**
 * this is component meter size
 * @param meterSizeId
 * @param displayElectmentId
 */
function meterSize(meterSizeId , displayElectmentId){
	$.getJSON('MeterSizeComponentAPISrvl', {
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกขนาดมาตร--</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(cityCode == result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#"+displayElectmentId).html(options);
	});
}

/**
 * this is load data from register data
 */
function loadDataStepTwo(){

	$("#stepTwoInputPname").val($("#stepOneInputPName").val());
	
	var stepOneName  = $("#stepOneInputName").val();
	var arrName = stepOneName.split(" ");
    
	$("#stepTwoInputPname").val(arrName[0]);
	$("#stepTwoInputFname").val(arrName[1]);
    $("#stepTwoInputLname").val(arrName[2]);

	$("#stepTwoInputHomeNumber").val($("#stepOneInputHomeNumber").val());
	$("#stepTwoInputAddress").val($("#stepOneInputAddress").val());
	
	var stepOneProvince = $("#stepOneInputProvinceCode option:selected").val();
	
	$("#stepTwoInputProvinceCode option[value=" + stepOneProvince + "]").attr('selected','selected');
	
	var stepOneDistirct  = $("#stepOneInputDistrictCode option:selected").val();
	
	district(stepOneProvince, stepOneDistirct , "stepTwoInputDistrictCode");
	
	var stepOneCityCode  = $("#stepOneInputCityCode option:selected").val();
	
	cityCode(stepOneProvince, stepOneDistirct, stepOneCityCode , "stepTwoInputCityCode");
		
	$("#stepTwoInputPostCode").val($("#stepOneInputPostCode").val());
	$("#stepTwoInputTelephoneUser").val($("#stepOneInputTelephone").val());

}

/**
 * this is load data from to contact data
 */
function loadDataStepTwo2(){ 
	
	$("#stepTwoInputNameContact").val($("#stepOneInputName").val());
	
	$("#stepTwoInputHomeNumberContact").val($("#stepTwoInputHomeNumber").val());
	
	$("#stepTwoInputAddressContact").val($("#stepTwoInputAddress").val());
	
	$("#stepTwoInputTelephoneContact").val($("#stepTwoInputTelephoneUser").val());
	
	$("#stepTwoInputPostCodeContact").val($("#stepTwoInputPostCode").val());
	
	var stepOneProvince = $("#stepTwoInputProvinceCode option:selected").val();
	
	$("#stepTwoInputProvinceCodeContact option[value=" + stepOneProvince + "]").attr('selected','selected');
	
	var stepOneDistirct  = $("#stepTwoInputDistrictCode option:selected").val();
	
	district(stepOneProvince, stepOneDistirct , "stepTwoInputDistrictCodeContact");
	
	var stepOneCityCode  = $("#stepTwoInputCityCode option:selected").val();
	
	cityCode(stepOneProvince, stepOneDistirct, stepOneCityCode , "stepTwoInputCityCodeContact");
	
}

/**
 * calculate money
 */
function calculateMoney($action,type){
	$('input[name=stepThreeInputCalculateMoney][value=Y]').prop("checked",true);
	var meterSizeCode  =  0;
	
	if($("#stepThreeInputMeterSize option:selected").val() != ""){ 
		meterSizeCode  = $("#stepThreeInputMeterSize option:selected").val();
	} else { 
		meterSizeCode  =  0;
	}
	
	if($action == 'Y'){
	 	$.getJSON('CS61001Srvl',{
			process_type : "calculate" , 
			meterSizeCode : meterSizeCode
		}, function(result) {
			
			if(result.length!=0){
				var html =  "";
				for (var i = 0; i < result.length; i++) {
					html += '<div class="form-group">';
					html += '<label class="col-sm-3 control-label"><span></span></label>';
					html += '<div class="col-sm-4">';
					html +=  result[i].RECEIVE_CODE + '   ' + result[i].DESCRIPTION;
					html += "<input type='hidden' name='stepThreeInputReceive' id='stepThreeInputReceive' value='" + result[i].RECEIVE_CODE + "'/>";
					html += '</div>';
					html += '<div class="col-sm-2">';
					html += ' <input type="text" class="form-control" name="stepThreeInputCalculateMoneyAmount" id="stepThreeInputCalculateMoneyAmount" value="' + result[i].FEE + '" />';
					html += '</div>';
					html += '<div class="col-sm-1">';
					html += '<label class="col-sm control-label">บาท</label>';
					html += '</div>';
					html += '</div>';
				}
				$('input[name=stepThreeInputCalculateMoney][value=Y]').prop("checked",true);
				$("#DisplayCalculateMoney").html(html);
			}else{
				if(type==2){
					$("html,body").animate({scrollTop: 0}, 100);
					$(window).scrollTop($("#error_message").offset().top);
					$("#content_error").text("ไม่มีการคิดเงิน ในขนาดมาตรนี้");
					$('#error_message').removeClass('hide');
				}
				
				$('input[name=stepThreeInputCalculateMoney][value=N]').prop("checked",true);
				$("#DisplayCalculateMoney").html("");
			}
		});
	} else { 
		$('input[name=stepThreeInputCalculateMoney][value=N]').prop("checked",true);
		$("#DisplayCalculateMoney").html("");
	}
}

function getRequest(){
	var vaRequestNumber = $("#inputKeyWordSearch").val();
	$('#error_message').addClass('hide');
	$('#success_message').addClass('hide');

	$
	.ajax({
		url : "CS61001Srvl?process_type=search",
		type : "POST",
		data : {
			
			request_number : vaRequestNumber,
	
		},
		async : false,
		dataType : 'json',
		success : function(result) {
			
			if(result.message=="no"){
				$("#content_error").text("ไม่พบเลขที่คำร้อง กรุณาระบุใหม่อีกครั้ง");
				$('#error_message').removeClass('hide');
				
				$("#process_type").val("insert");
				$("#stepOneInputName").val("");
				$("#stepOneInputHomeNumber").val("");
				$("#stepOneInputAddress").val("");
				
				setDefaultAddress();
				
				$("#stepOneInputTelephone").val("");
				
				
				$("#stepTwoInputPname").val("");
				$("#stepTwoInputFname").val("");
				$("#stepTwoInputLname").val("");
				$("#stepTwoInputIdCard").val("");
				$("#stepTwoInputHomeCardId").val("");
				$("#stepTwoInputHomeNumber").val("");
				$("#stepTwoInputAddress").val("");
				$("#stepTwoInputTelephoneUser").val("");
				
				$("#stepTwoInputNameContact").val("");
				$("#stepTwoInputHomeNumberContact").val("");
				$("#stepTwoInputAddressContact").val("");
				$("#stepTwoInputTelephoneContact").val("");
				
				$("#stenTwoInputGarbageCharge").val("");
				$("#stepTwoInputWasteWaterCharge").val("");
				$("#stepTwoInputOtherCharge").val("");
				
				$("select#stepThreeInputCustomerType").val("");
				$("select#stepThreeInputCustomerClass").val("");
				$("select#stepThreeInputAllowance").val("");
				
				$(":checkbox[name='stepThreeInputDocument']").attr('checked', false);

				$("#stepThreeInputComment").val("");
				
				$("select#stepThreeInputMeterSize").val("");
				
				$('input[name=stepThreeInputCalculateMoney][value=N]').prop("checked",true);
				$("#DisplayCalculateMoney").html("");
			}else{

				$("#process_type").val("update");
				$("#stepOneInputName").val(result.applicant_name);
				$("#stepOneInputHomeNumber").val(result.applicant_home_number);
				$("#stepOneInputAddress").val(result.applicant_address);
				
				$("#stepOneInputPostCode").val(result.applicant_post_code);
				$("#stepTwoInputPostCode").val(result.post_code);
				$("#stepTwoInputPostCodeContact").val(result.contact_post_code);
				setTimeout( function(){ province( result.applicant_province_code,"stepOneInputProvinceCode");}, 300);
				setTimeout( function(){ province( result.province_code,"stepTwoInputProvinceCode");}, 300);
				setTimeout( function(){ province( result.contact_province_code,"stepTwoInputProvinceCodeContact");}, 300);
				setTimeout( function(){ district( result.applicant_province_code,result.applicant_district_code,"stepOneInputDistrictCode");}, 300);
				setTimeout( function(){ district( result.province_code,result.district_code,"stepTwoInputDistrictCode");}, 300);
				setTimeout( function(){ district( result.contact_province_code,result.contact_district_code,"stepTwoInputDistrictCodeContact");}, 300);
				setTimeout( function(){ cityCode( result.applicant_province_code,result.applicant_district_code,result.applicant_city_code,"stepOneInputCityCode");}, 300);
				setTimeout( function(){ cityCode( result.province_code,result.district_code,result.city_code,"stepTwoInputCityCode");}, 300);
				setTimeout( function(){ cityCode( result.contact_province_code,result.contact_district_code,result.contact_city_code,"stepTwoInputCityCodeContact");}, 300);
				
				$("#stepOneInputTelephone").val(result.applicant_telephone);
				
				
				$("#stepTwoInputPname").val(result.pname);
				$("#stepTwoInputFname").val(result.fname);
				$("#stepTwoInputLname").val(result.lname);
				$("#stepTwoInputIdCard").val(result.card_id);
				$("#stepTwoInputHomeCardId").val(result.house_id);
				$("#stepTwoInputHomeNumber").val(result.home_number);
				$("#stepTwoInputAddress").val(result.address);
				$("#stepTwoInputTelephoneUser").val(result.telephone);
				
				$("#stepTwoInputNameContact").val(result.contact_name);
				$("#stepTwoInputHomeNumberContact").val(result.contact_home_number);
				$("#stepTwoInputAddressContact").val(result.contact_address);
				$("#stepTwoInputTelephoneContact").val(result.contact_telephone);
				
				$("#stenTwoInputGarbageCharge").val(result.garbage_charge);
				$("#stepTwoInputWasteWaterCharge").val(result.wastewate_charge);
				$("#stepTwoInputOtherCharge").val(result.other_charge);
				
				$("select#stepThreeInputCustomerType").val(result.customer_type);
				$("select#stepThreeInputCustomerClass").val(result.customer_class);
				$("select#stepThreeInputAllowance").val(result.allowance);
				
				if(result.document_type != null){
					var arrDataType = result.document_type.split(",");
					jQuery.each( arrDataType, function( i, val ) {
						$(":checkbox[name='stepThreeInputDocument'][value='"+val+"']").attr('checked', true);
					});
				}

				$("#stepThreeInputComment").val(result.document_reference);
				
				$("select#stepThreeInputMeterSize").val(result.meter_size);

				
			
				if(result.receive_code.length!=0){
					
					$('input[name=stepThreeInputCalculateMoney][value=Y]').prop("checked",true);
					
					var html =  "";
					for (var i = 0; i < result.receive_code.length; i++) {
						html += '<div class="form-group">';
						html += '<label class="col-sm-3 control-label"><span></span></label>';
						html += '<div class="col-sm-4">';
						html +=  result.receive_code[i] + '   ' + result.receive_desc[i];
						html += "<input type='hidden' name='stepThreeInputReceive' id='stepThreeInputReceive' value='" + result.receive_code[i] + "'/>";
						html += '</div>';
						html += '<div class="col-sm-2">';
						html += ' <input type="text" class="form-control" name="stepThreeInputCalculateMoneyAmount" id="stepThreeInputCalculateMoneyAmount" value="' + result.receive_amount[i] + '" />';
						html += '</div>';
						html += '<div class="col-sm-1">';
						html += '<label class="col-sm control-label">บาท</label>';
						html += '</div>';
						html += '</div>';
					}
					$("#DisplayCalculateMoney").html(html);
				}else{
					$('input[name=stepThreeInputCalculateMoney][value=N]').prop("checked",true);
					$("#DisplayCalculateMoney").html("");
				}
				

			}
			
		}
	});
}

